<?php
namespace app\admin\middleware;

use app\model\AdminUser;
class Check
{
    public $noValidate = ['/admin/login'];
    public function handle($request, \Closure $next)
    {
        if(in_array(strtolower($request->url()), $this->noValidate)){
            // 无需检测token规则的路由
            return $next($request);
        }
        $token = $request->header()['access-token'] ? $request->header()['access-token'] : '';
        $admin = new AdminUser();
        $str = $admin->decode($token);// 解码
        $strArr = explode('|',$str);
        if(count($strArr) != 2){
            return json(['code' => 401 ,'msg' => '登录参数不正确!']);
        }
        $id = $strArr[0];
        $username = $strArr[1];
        if(!$admin->testUserAndId($id , $username)){
            return json(['code'=> 401 , 'msg' => '登录参数不正确!']);
        }
        $request->id = $id;
        $request->username = $username;
        return $next($request);
    }
}